﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using linkedin_bot_demo.Models;
namespace linkedin_bot_demo.ViewModels
{
    using System.Windows.Input;
    using System.Xml.Serialization;
    using linkedin_bot_demo.Common;
    using System.IO;
    using linkedin_bot_demo.Service;

    public class AccountsViewModel : ViewModel
    {
        private readonly static XmlSerializer AccountsSerializer = new XmlSerializer(typeof(AccountsModel));

        public AccountsViewModel()
        {
            try
            {
                LoadAccounts();
            }
            catch
            {
                //this.Accounts.Add(new AccountModel()
                //{
                //    Email = "maxim.anurin@gmail.com",
                //    Expires = 123,
                //    Token = "AQXKKKoWlWDqWWGmJzdQbrV0lHv2KJ7A_nxmOBlRZNQQCzZusP0SvWjqD4eUVoYuR3qSwz807eIBJz1S1cDEaRYKzc"
                //});
            }
        }

        private AccountModel _selectedUser;
        public AccountModel SelectedUser
        {
            get { return _selectedUser; }
            set
            {
                _selectedUser = value;
                OnPropertyChanged("SelectedUser");
            }
        }

        private readonly ObservableCollection<AccountModel> _accounts = new ObservableCollection<AccountModel>();
        public ObservableCollection<AccountModel> Accounts { get { return this._accounts; } }

        private DelegateCommand<AccountModel> _deleteAccountCommand;
        public ICommand DeleteAccountCommand
        {
            get { return this._deleteAccountCommand ?? (this._deleteAccountCommand = new DelegateCommand<AccountModel>(OnDeleteAccountExecute)); }
        }
        public void OnDeleteAccountExecute(AccountModel account)
        {
            this.Accounts.Remove(account);

            this.SaveAccounts();
        }

        private DelegateCommand<AccountModel> _addAccountCommand;
        public ICommand AddAccountCommand
        {
            get { return this._addAccountCommand ?? (this._addAccountCommand = new DelegateCommand<AccountModel>(OnAddAccountExecute)); }
        }
        public void OnAddAccountExecute(AccountModel account)
        {
            using (LinkedinApiClient api = new LinkedinApiClient())
            {
                MyProfileModel myProfile = api.GetMyProfile();

                this.Accounts.Add(new AccountModel()
                {
                    FirstName = myProfile.FirstName,
                    LastName = myProfile.LastName,
                    Expires = api.Expires,
                    AccessToken = api.AccessToken
                });
            }

            this.SaveAccounts();
        }

        private void SaveAccounts()
        {
            AccountsModel xmlModel = new AccountsModel() { Accounts = this.Accounts.ToArray() };
            using (FileStream fs = new FileStream(((App)App.Current).AccountsFile, FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                AccountsSerializer.Serialize(fs, xmlModel);

                fs.Close();
            }
        }

        private void LoadAccounts()
        {
            using (FileStream fs = new FileStream(((App)App.Current).AccountsFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                AccountsModel xmlModel = (AccountsModel)AccountsSerializer.Deserialize(fs);

                this.Accounts.Clear();
                if (xmlModel.Accounts != null)
                {
                    foreach (AccountModel account in xmlModel.Accounts)
                    {
                        this.Accounts.Add(account);
                    }
                }
            }
        }
    }
}